class Solution:
    def minFlipsMonoIncr(self, S: str) -> int:
        res = 0
        one_count = int(S[0])
        for i in range(1, len(S)):
            if S[i] == '1':
                one_count += 1
            elif S[i] == '0':
                res = min(res + 1, one_count)

        return res
